import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

public class Serializer
{
    public static byte[] objectToByteArray(Object object)
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutput out;
        try
        {
            out = new ObjectOutputStream(bos);
            out.writeObject(object);
            byte[] yourBytes = bos.toByteArray();

            out.close();
            bos.close();

            return yourBytes;
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

    }

    public static Object byteArrayToObject(byte[] data)
    {
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        ObjectInput in;
        try
        {
            in = new ObjectInputStream(bis);
            Object object = in.readObject();

            bis.close();
            in.close();
            return object;
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

    }
}
